package com.example.accessibilityhelper.fetchcoporation

import android.accessibilityservice.AccessibilityService
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import com.example.accessibilityhelper.Step
import com.example.accessibilityhelper.config.Id
import com.example.accessibilityhelper.config.Page
import com.example.accessibilityhelper.util.findAccessibilityNodeByShortViewId
import com.example.accessibilityhelper.util.isWindowStateChanged

class Step1CollectCorporationList: Step() {
    private val TAG = "Step1CollectCorporation"

    override fun match(service: AccessibilityService, event: AccessibilityEvent): Boolean {
        return event.isWindowStateChanged()
                && event.className == Page.main_corporation_list_page
    }

    override fun execute(service: AccessibilityService, event: AccessibilityEvent) {
        val recyclerViewNode =
            service.rootInActiveWindow.findAccessibilityNodeInfosByViewId(Id.main_corporation_recyclerview).firstOrNull()
        val childCount = recyclerViewNode?.childCount?:0
        val corporations = arrayListOf<String>()
        for(index in 0 until childCount){
            val itemText = recyclerViewNode!!.getChild(index).findAccessibilityNodeInfosByViewId(Id.main_corporation_item).first().text.toString()
            Log.i(TAG, "execute: find a corporation ${itemText}")
            corporations.add(itemText)
        }
//        submit(corporations)
        finish()
    }
}